<template>
    <!-- 引入container布局 -->
    <el-container class="home_container">
        <el-header>
            <div>
                <img src="../assets/zjljr-三日思.jpg" alt="" />
                <span>个人运动平台</span>
            </div>
            <el-button type="info" @click="logout">安全退出</el-button>
        </el-header>
        <el-container>
            <el-aside :width="isCollapse?'64px':'200px'">
                <div class="toggle_button" @click="toggleCollapse()">|||</div>
                <!-- unique-opened表示只展开一个 -->
                <el-menu
                    background-color="#545c64"
                    text-color="#fff"
                    active-text-color="#409eff"
                    unique-opened
                    :collapse="isCollapse"
                    :collapse-transition="false"
                    :router="true"
                    :default-active="activePath"
                >
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id" v-for="item in menuList" :key="item.id">
                        <template slot="title">
                            <i :class="iconObject[item.id]"></i>
                            <span>{{item.title}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="it.path" v-for="it in item.sList" :key="it.id" @click="saveNavState(it.path)">
                            <template slot="title">
                                <i :class="iconObject[it.id]"></i>
                                <span>{{it.title}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <router-view></router-view>           
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
export default {
    data() {
        return {
            // 菜单列表
            menuList: [],
            isCollapse: false,// 伸缩
            iconObject:{
                '100':'iconfont icon-denglu',
                '200':'iconfont icon-yundong',
                '101':'iconfont icon-denglu',
                '102':'iconfont icon-mima',
                '103':'iconfont icon-yundong',
                '104':'iconfont icon-shangpin',
                '201':'iconfont icon-shu',
                '202':'iconfont icon-qialuli',
                '203':'iconfont icon-shiwu',
            },
            activePath:'/welcome',// 默认路径
        };
    },
    // onload事件
    created() {
        // 查询menuList
        this.getMenuList();
        this.activePath = window.sessionStorage.getItem("activePath");// 取出session里面的path，动态修改activePath 
    },
    methods: {
        // 安全退出
        logout() {
            window.sessionStorage.clear(); // 清除session
            this.$router.push("/login"); //回到登录页
        },
        // 获取导航栏菜单
        async getMenuList(){
            const {data:res} = await this.$http.get("menus");
            // console.log(res);
            if(res.flag != 200){
                return this.$$message.console.error("获取菜单列表失败");// 访问失败
            }else{
                this.menuList = res.menus;// 访问成功数据回填
            }
        },
        // 控制菜单伸缩
        toggleCollapse(){
            this.isCollapse = !this.isCollapse;
        },
        // 点击二级菜单保存路径
        saveNavState(activePath){
            window.sessionStorage.setItem("activePath",activePath);// 存放在session里
            this.activePath = activePath;
        },
    },
};
</script>
<style lang="less" scoped>
.home_container {
    height: 100%;
}
// 头部
.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between; // 左右贴边
    padding-left: 0%;
    color: #fff;
    font-size: 20px;
    align-items: center;
    > div {
        display: flex;
        align-items: center;
        span {
            margin-left: 15px;
        }
        img {
            width: 55px;
            height: 55px;
        }
    }
}
// 侧边栏
.el-aside {
    background-color: #333744;
    .el_menu{
        border-right: none;
    }
}
// 主体样式
.el-main {
    background-color: #eaedf1;
}
// |||按钮样式
.toggle_button{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>